/*
 * MPEGFile.h
 *      -- file holding a complete MPEG encoded data
 *
 * Author: Qiu Shuang
 * Date: Mar 1st, 2011
 */
#ifndef MPEGFILE_H
#define MPEGFILE_H

#include <stdio.h>
#include <list>

#define HEADER 4            /* frame header size in bytes */

/* forward class declaration */
class MPEGFrameData;

/* main class declaration */
class MPEGFile{
public:
    /*
     * constructor, take an mp3 filename as input
     * and open specified file, an additional boolean
     * value indicating file needs parsed instinctively
     */
    MPEGFile(const char *file_name, bool parse = true);

    /* destructor, close mpeg file if open */
    ~MPEGFile();

    /* skip current frame and move file cursor next frame content
     * extract frame header into frame header buffer
     * NOTE: this function must be used accompnied with parse_header,
     *       parse_data, etc.
     */
    int nextFrame();

    /* field accessor */
    const char *filename();
    FILE *mpeg();
    const unsigned char *headerBuffer();
    const std::list<MPEGFrameData *> &contents();
    int numberOfGranules();

private:
    FILE *_mpeg;             /* mpeg file on local fs */
    const char *_filename;   /* file name */

    long _content_begin;     /* position indicator of the stream point to
                              * where content begins */
    unsigned char _header_buf[HEADER];/* header buffer */

    /* list of pointers to MPEG frame data */
    std::list<MPEGFrameData *> _contents;
};

/* field accessor */
inline const char *MPEGFile::filename(){
    return _filename;
}

inline FILE *MPEGFile::mpeg(){
    return _mpeg;
}

inline const unsigned char *MPEGFile::headerBuffer(){
    return _header_buf;
}

inline const std::list<MPEGFrameData *> &MPEGFile::contents(){
    return _contents;
}

inline int MPEGFile::numberOfGranules(){
    return _contents.size();
}

#endif